package logik;

import gui.Screen;

import java.text.NumberFormat;
import java.util.Calendar;
import javax.swing.JOptionPane;

/**
 * @author Matthias
 *
 */
public class InterestRate extends Transaction {

	private double interestRate;
	private NumberFormat nf = NumberFormat.getInstance();

	public InterestRate(PerformTransactions perform){
		super(perform);
		nf.setMaximumFractionDigits(2);	
	}
	
	public double getInterestRate(String accountType){
		
		if (accountType.equals("Credit")) {
			this.interestRate = 0.0125;
		}
		else if (accountType.equals("Savings")) {
			this.interestRate = 0.0250;
		}
		else if (accountType.equals("Depot")) {
			this.interestRate = 0.0450;
		}
		return interestRate;
	}
	
	@Override
	public void execute(){
		getClassPerform().getClassATM().getClassScreen().getAccInterestRate().showTextArea(executeIR());
	}
	
	public String executeIR(){

		StringBuilder sb = new StringBuilder();
		getClassPerform().getClassATM().getClassScreen().show(Screen.ACCINTERESTRATE);
		int endyear = 
				getClassPerform().getClassATM().getClassScreen().getAccInterestRate().getEndYear(); 
		// retrieves the current year of the computer calendar
		int currentYear = 
				Calendar.getInstance().get(Calendar.YEAR); // Gets the current year from the computer
		// gets the current balance of the chosen account
		double currentBalance = 
				getClassPerform().getClassATM().getClassBankDatabase().getTotalBalance(getCPR_ID());

		if (endyear <= (currentYear+10) && endyear >= currentYear) {
			for (int year = currentYear; year<=endyear; year++) {
				double balance = currentBalance * Math.pow(1 + getInterestRate(getAccType()), year-currentYear);
				sb.append(nf.format(balance) + " - " + year + "\n");
			}
		} // end else if
		else if (endyear < currentYear || endyear > (currentYear+10)){
			JOptionPane.showMessageDialog(null, "Invalid input, You must enter an integer from "
					+ currentYear + " until " + (currentYear+10), "Error!", JOptionPane.ERROR_MESSAGE);
		}
		return sb.toString();
	} // end method

} // end class InterestRate